package com.kedacom.freedomLabel.view.activity.category;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;

import com.kedacom.freedomLabel.Options;
import com.kedacom.freedomLabel.R;
import com.kedacom.freedomLabel.base.BaseActivity;
import com.kedacom.freedomLabel.databinding.ActivityCategoryNewCategoryControlBinding;
import com.kedacom.freedomLabel.initdata.bean.CategoryNewBean;
import com.kedacom.freedomLabel.initdata.dao.CategoryNewDao;
import com.kedacom.freedomLabel.model.DataControlResult;
import com.kedacom.freedomLabel.model.bean.Category;
import com.kedacom.freedomLabel.model.db.AppDatabase;
import com.kedacom.freedomLabel.model.db.dao.CategoryDao;
import com.kedacom.freedomLabel.view.fragment.ListDataFragment;
import com.kedacom.freedomLabel.viewmodel.category.NewCategoryControlViewModel;
import com.kedacom.lego.annotation.ContentView;
import com.kedacom.lego.annotation.OnMessage;
import com.kedacom.lego.annotation.ViewModel;
import com.kedacom.lego.fast.validation.RegexTemplate;
import com.kedacom.lego.message.LegoMessageManager;

import java.util.regex.Pattern;


@ContentView(R.layout.activity_category_new_category_control)
@ViewModel(NewCategoryControlViewModel.class)
public class NewCategoryControlActivity extends BaseActivity<ActivityCategoryNewCategoryControlBinding, NewCategoryControlViewModel> {

    CategoryNewBean mSelectedItem;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        closeEditLayout();
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        CategoryNewDao categoryDao = AppDatabase.getInstance().getCategoryNewDao();
        ListDataFragment<CategoryNewBean> fragment = ListDataFragment.newInstance(categoryDao);
        transaction.add(mBinding.recyclerContainer.getId(), fragment);
        transaction.commit();
    }

    /**
     * 接收数据选择
     *
     * @param item 选择的Item
     */
    @OnMessage(value = Options.MSG_SELECT_ITEM, discard = true)
    public void receiveItemClickMessage(CategoryNewBean item) {
        closeEditLayout();
        if(!item.isInit){
            showToast("此数据为初始数据，不可修改",TOAST_WARNING);
            return;
        }
        mSelectedItem = item;
        mViewModel.rubbishName.set(item.name);

    }

    /**
     * 接收数据更新结果通知
     *
     * @param result 结果
     */
    @OnMessage(value = Options.MSG_SAVE_ITEM_RESULT, discard = true)
    public void receiveDataSaveResult(DataControlResult<CategoryNewBean> result) {
        if (result.result) {
            closeEditLayout();
            mSelectedItem = result.data;
        }
    }

    /**
     * 接收数据删除结果通知
     *
     * @param result 结果
     */
    @OnMessage(value = Options.MSG_DELETE_ITEM_RESULT, discard = true)
    public void receiveDataDeleteResult(DataControlResult<Category> result) {
        if (result.result) {
            clear();
        }
    }

    /**
     * 接收新增数据结果通知
     *
     * @param result 结果
     */
    @OnMessage(value = Options.MSG_ADD_ITEM_RESULT, discard = true)
    public void addItemComplete(DataControlResult<Category> result) {
        if (result.result) {
            closeEditLayout();
        }
    }

    /**
     * 接收布局清空通知
     */
    @OnMessage(value = Options.MSG_RECOVERY_ITEM_DETAIL, discard = true)
    public void recoveryDetailLayout() {
        mBinding.save.setTag(null);
        clear();
        closeEditLayout();
    }

    // 保存
    public void onSaveIDViewClick(View view) {
        beep();
        String name = mBinding.rubbishName.getText().toString();

        if (!Pattern.compile(RegexTemplate.NOT_EMPTY).matcher(name).matches()) {
            showToast(getString(R.string.validation_empty_name), TOAST_WARNING);
            return;
        }

        Object tag = mBinding.save.getTag();
        if (tag != null) {
            mViewModel.updateID(mSelectedItem);
        } else {
            mViewModel.saveID();
        }
    }

    // 删除
    public void onDeleteItemButtonClick(View view) {
        beep();

        showDialog("是否删除", "删除", (dialog, which) -> {
            LegoMessageManager.getInstance().sendEmptyMessage(Options.MSG_DELETE_ITEM);
            dialog.dismiss();
        });
    }

    // 增加
    public void onAddItemButtonClick(View view) {
        beep();
        openEditLayout();
        // 标识新增
        mBinding.save.setTag(null);
        clear();
    }

    // 编辑
    public void onEditIDViewClick(View view) {
        beep();
        if (mSelectedItem != null) {
            openEditLayout();
            // 标识编辑
            mBinding.save.setTag(mSelectedItem.getId());
        }
    }

    // 清除
    public void onClearViewClick(View view) {
        beep();
        clear();
    }

    // 返回
    public void onCancelViewClick(View view) {
        beep();
        closeEditLayout();
        mBinding.save.setTag(null);
    }

    // 退出
    public void onBackButtonClick(View view) {
        beep();
        finish();
    }

    private void clear() {
        mBinding.save.setTag(null);
        mViewModel.rubbishName.set(null);

    }

    private void closeEditLayout() {

        mBinding.controlAddLayout.setVisibility(View.VISIBLE);
        mBinding.controlSaveLayout.setVisibility(View.GONE);
    }

    private void openEditLayout() {
        mBinding.rubbishName.setEnabled(true);
        mBinding.controlAddLayout.setVisibility(View.GONE);
        mBinding.controlSaveLayout.setVisibility(View.VISIBLE);
    }
}